package com.lanswon.qzsmk.controller.cardManageController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ch.qos.logback.classic.Logger;

import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.exception.SystemException;
import com.lanswon.qzsmk.model.Collection;
import com.lanswon.qzsmk.model.Nation;
import com.lanswon.qzsmk.model.Personal;
import com.lanswon.qzsmk.service.cardManageService.ApplyCardService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.util.pdfutil.TemplateAppRegPDF;
import com.lanswon.qzsmk.vo.ResultVo;

/**
 * 描述：打印申请表单
 * 创建日期：20180212
 * 版本：v1.0 初版
 * @author 惠国磊
 *
 */

@Controller
@RequestMapping(value="/printApplicationForm")
public class ApplicationFormPrintController {
	/*logger对象*/
	private final static Logger logger = (Logger) LoggerFactory.getLogger(ApplicationFormPrintController.class);
	
	@Autowired
	ParamManageService pms;
	
	@Autowired
	ApplyCardService acs;
	
	
	@RequestMapping(value="/toPdfView",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> appPDFCreate(HttpServletRequest request){
		Map<String,Object> map=new HashMap<String,Object>();
		logger.info("appPDFCreate########start()");
		 logger.debug("打印--------根目录"+ request.getContextPath());
	     logger.debug("打印-------根目录1"+ request.getSession().getServletContext().getContextPath());
		//URLDecoder.decode(request.getParameter("param"),"utf-8");
		String paramString = null;
		try {
			paramString = URLDecoder.decode(request.getParameter("param"),"utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			map.put("statusCode", "300");
			map.put("message", "转码异常");
			return map;
		}
		logger.debug("dialogParam==>"+paramString);
		//解析传过来的参数
		Map<String,String> paramMap = new HashMap<String,String>();
		String[] paramArry = paramString.split("&");
		for(int i=0;i<paramArry.length;i++){
			String param = paramArry[i];
			String[] mapArray = param.split("=");
			if(mapArray.length>1){
				paramMap.put(mapArray[0], mapArray[1]);
			}else{
				paramMap.put(mapArray[0], "");
			}
			
		}
		logger.debug("解析传过来的参数装成的map==>"+paramMap);
		Map<String,String> workMap = new HashMap<String,String>();
		//获取画面的个人信息
		
		
		
		String isPhoto = paramMap.get("isPhoto");
		String collectionPoint = paramMap.get("collectionpoint");
		String employeeID = paramMap.get("employeeID");
		String cardpName = paramMap.get("cardpName");
		String cardCode = paramMap.get("cardCode");
		String cardtype = paramMap.get("cardtype");
		String cardTypeCode = paramMap.get("cardTypeCode");
		String customerName = paramMap.get("customerName");
		String gender = paramMap.get("gender");
		String nationality = paramMap.get("nationality");
		String birth = paramMap.get("birth");
		String papertype = paramMap.get("papertype");
		String paperNo = paramMap.get("paperNo");
		String prAddrProvince = paramMap.get("prAddrProvince"); 
		String prAddrCity = paramMap.get("prAddrCity");
		
		//现居住地址
		String curAddrProvince = paramMap.get("curAddrProvince");
		String curAddrCity = paramMap.get("curAddrCity");
		String curAddrCounty = paramMap.get("curAddrCounty");
		String curAddrStreet = paramMap.get("curAddrStreet");
		String curAddrDetailedInfo = paramMap.get("curAddrDetailedInfo");
		
		//通讯地址
		String contactAddrProvince = paramMap.get("contactAddrProvince");
		String contactAddrCity = paramMap.get("contactAddrCity");
		String contactAddrCounty = paramMap.get("contactAddrCounty");
		String contactAddrStreet = paramMap.get("contactAddrStreet");
		String contactAddrDetailedInfo = paramMap.get("contactAddrDetailedInfo");
		String homePhone = paramMap.get("homePhone");
		//20181203 惠国磊  修改以下1行  
		String mobile = paramMap.get("mobile");
		String contactPostalcode = paramMap.get("contactPostalcode");
		String email = paramMap.get("email");
		String date = (new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss ")).format(new Date());
		
		//1.cardNo为市民卡的卡面号后8位，这里先写死。
		String pdfName = paramMap.get("paperNo")+"_"+cardTypeCode+"_"+cardCode;
		/*if("".equals(pdfName)||null==pdfName){
			logger.error("市民卡号为空！");
			//throw exception;
		}*/
		request.getSession().removeAttribute("pdfName");
		request.getSession().setAttribute("pdfName", pdfName);
		request.getSession().removeAttribute("paperNo");
		request.getSession().setAttribute("paperNo", paperNo);
		
		logger.debug("collectionPoint==>"+collectionPoint);
		logger.debug("employeeID==>"+employeeID);
		logger.debug("cardpName==>"+cardpName);
		logger.debug("cardCode==>"+cardCode);
		logger.debug("cardtype==>"+cardtype);
		logger.debug("cardTypeCode==>"+cardTypeCode);
		logger.debug("customerName==>"+customerName);
		logger.debug("gender==>"+gender);
		logger.debug("nationality==>"+nationality);
		logger.debug("birth==>"+birth);
		logger.debug("papertype==>"+papertype);
		logger.debug("paperNo==>"+paperNo);
		logger.debug("prAddrProvince==>"+prAddrProvince);
		logger.debug("prAddrCity==>"+prAddrCity);
		logger.debug("curAddrProvince==>"+curAddrProvince);
		logger.debug("curAddrCity==>"+curAddrCity);
		logger.debug("curAddrCounty==>"+curAddrCounty);
		logger.debug("curAddrStreet==>"+curAddrStreet);
		logger.debug("curAddrDetailedInfo==>"+curAddrDetailedInfo);
		logger.debug("contactAddrProvince==>"+contactAddrProvince);
		logger.debug("contactAddrCity==>"+contactAddrCity);
		logger.debug("contactAddrCounty==>"+contactAddrCounty);
		logger.debug("contactAddrStreet==>"+contactAddrStreet);
		logger.debug("contactAddrDetailedInfo==>"+contactAddrDetailedInfo);
		logger.debug("homePhone==>"+homePhone);
		logger.debug("mobile==>"+mobile);
		logger.debug("contactPostalcode==>"+contactPostalcode);
		logger.debug("email==>"+email);
		logger.debug("date==>"+date);
		
		Personal p = new Personal();
		p.setCustomerName(customerName);
		p.setPaperNo(paperNo);
		p.setPaperType(papertype);
		p.setOtherInfo01(isPhoto);
		ResultVo vo = acs.saveCollection(p,cardCode);
		if(vo.getResultStatus().getCode() == HttpContants.RETURN_SUCCESS){
			Collection oo = (Collection) vo.getServiceObject();
			workMap.put("collectionNo", oo.getCollectionNo());
			request.getSession().setAttribute("CollectionId",oo.getCollectionId());
			request.getSession().setAttribute("flag", true);
		}else{
			
			map.put("statusCode", "300");
			map.put("message", vo.getResultStatus().getMessage());
			return map;
		}
		
		workMap.put("collectionpoint", collectionPoint);
		workMap.put("employeeID", employeeID);
		workMap.put("cardpName", cardpName);
		workMap.put("cardtype", cardtype);
		workMap.put("customerName", customerName);
		workMap.put("gender", gender);
		
		String nationName="汉族";
		ResultVo rv = pms.queryNationByCode(nationality);
		if(rv.getResultStatus().getCode() == HttpContants.RETURN_SUCCESS){
			Nation n = (Nation) rv.getServiceObject();
			nationName  = n.getNationname();
		}
		workMap.put("nationality", nationName);
		workMap.put("birth", birth);
		workMap.put("papertype", papertype);
		workMap.put("paperNo", paperNo);
		workMap.put("prAddrProvince", prAddrProvince);
		workMap.put("prAddrCity", prAddrCity);
		workMap.put("curAddrProvince", curAddrProvince);
		workMap.put("curAddrCity", curAddrCity);
		workMap.put("curAddrCounty", curAddrCounty);
		workMap.put("curAddrStreet", curAddrStreet);
		workMap.put("curAddrDetailedInfo", curAddrDetailedInfo);
		workMap.put("contactAddrProvince", contactAddrProvince);
		workMap.put("contactAddrCity", contactAddrCity);
		workMap.put("contactAddrCounty", contactAddrCounty);
		workMap.put("contactAddrStreet", contactAddrStreet);
		workMap.put("contactAddrDetailedInfo", contactAddrDetailedInfo);
		workMap.put("homePhone", homePhone);
		workMap.put("mobile", mobile);
		workMap.put("contactPostalcode", contactPostalcode);
		workMap.put("email", email);
		workMap.put("pdfName", pdfName);
		workMap.put("date", date);
		
//		workMap.put("cardNo", cardNo);
		
		TemplateAppRegPDF appPDF = new TemplateAppRegPDF();
		
				//将画面的个人信息，传到模板，生成个人信息文件
					appPDF.createApplicationTicket(workMap,request);
		
		
		
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "printApplicationForm/appViewPage");
		map.put("dialogId", "appFormPrint");
		logger.info("appPDFCreate########end()");
		
		return map;
	} 
	
	@RequestMapping(value="/appViewPage",method=RequestMethod.POST)
    public String pdfView(){
		
		return "appViewPage";
		//return "pdfView";
	}
	
	@RequestMapping(value="/getPDFInfo",method=RequestMethod.GET)
	@ResponseBody
    public Map<String, Object> pdfInfo(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		HttpSession s = request.getSession();
		map.put("pdfName", s.getAttribute("pdfName"));
		map.put("paperNo", s.getAttribute("paperNo"));
		map.put("pdfPath", s.getAttribute("pdfPath"));
		request.getSession().removeAttribute("pdfName");
		request.getSession().removeAttribute("paperNo");
		return map;
		//return "pdfView";
	}
}
